<?php
/**
 * @package    indie-bookstore
 * @subpackage bookstore
 * @author David Neimeyer davidneimeyer@gmail.com
 * administrator/components/com_bookstore/controllers/bookstore.php
 * @link http://indie-bookstore.googlecode.com/
 * @license    GNU/GPL
 * 
 * @review this was modeled on the weblinks weblink controller;
 * some of the code/methods are probably hanging around; remove 
 * unused code
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

/**
 * bookstore bookstore Controller
 * 
 * for basic navigation to the other controllers
 * 
 * @package		indie-bookstore
 * @subpackage	bookstore
 * @review is this used? it shouldn't be needed, should it?
 *  all controller tasks are requested along with a specific,
 *  and not this, controller, right?  the api (as of 08/08)
 *  isn't interfaced, but that might be an except for this
 *  controller.
 * @note the indie-bookstore calendar might* use the api
 * in the backend, possibly necessitating this, but i doubt
 * the backend functionality that the api uses, actually calls
 * this controller (the model yes, the controller no). 
 */
class BookstoreControllerBookstore extends BookstoreController
{
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'add', 'edit' );
	}

	function edit()
	{
		JRequest::setVar( 'view', 'weblink' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();

		// Checkin the weblink
		$model = $this->getModel('bookstore');
		$model->checkout();
	}

	function save()
	{
		$post	= JRequest::get('post');
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$post['id'] = (int) $cid[0];

		$model = $this->getModel('bookstore');

		if ($model->store($post)) {
			$msg = JText::_( 'Weblink Saved' );
		} else {
			$msg = JText::_( 'Error Saving Weblink' );
		}

		// Check the table in so it can be edited.... we are done with it anyway
		$model->checkin();
		$link = 'index.php?option=com_bookstore';
		$this->setRedirect($link, $msg);
	}

	function remove()
	{
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('bookstore');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}


	function publish()
	{
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('bookstore');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}


	function unpublish()
	{
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('bookstore');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}

	function cancel()
	{
		// Checkin the weblink
		$model = $this->getModel('bookstore');
		$model->checkin();

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}


	function orderup()
	{
		$model = $this->getModel('bookstore');
		$model->move(-1);

		$this->setRedirect( 'index.php?option=com_bookstore');
	}

	function orderdown()
	{
		$model = $this->getModel('bookstore');
		$model->move(1);

		$this->setRedirect( 'index.php?option=com_bookstore');
	}

	function saveorder()
	{
		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel('bookstore');
		$model->saveorder($cid, $order);

		$msg = 'New ordering saved';
		$this->setRedirect( 'index.php?option=com_bookstore', $msg );
	}
}
?>
